package com.judian.smart.util;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.Log;

import com.judian.smart.base.MyApplication;

import java.io.IOException;

/**
 * author: Peter
 * date: 2019/08/29 星期四 18:40
 * desc:
 */
public class MusicPlayUtils implements MediaPlayer.OnCompletionListener {
    public static final int STOP_STATE = 0;
    public static final int PLAY_STATE = 1;
    public static final int PAUSE_STATE = 2;

    private Context context;
    private static MediaPlayer mediaPlayer;
    private static MusicPlayUtils instanceMedia;
    public static int playState;

    public static MusicPlayUtils getInstanceMedia() {
        if (instanceMedia == null) {
            instanceMedia = new MusicPlayUtils(MyApplication.getInstance());
        }
        return instanceMedia;
    }

    private MusicPlayUtils(Context context) {
        this.context = context;
        instanceMedia = this;
        init();
    }

    public void init() {
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setOnCompletionListener(this);
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer mp) {
                try {
                    mp.start();
                } catch (Exception e) {
                    System.out.println(e.toString());
                }
            }
        });
        mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Log.d(MusicPlayUtils.class.getSimpleName(), "播放出错");
                return true;
            }
        });
    }

    public void play(String url) {
        try {
            mediaPlayer.reset();
            mediaPlayer.setDataSource(context, Uri.parse(url));
            mediaPlayer.prepareAsync();
            mediaPlayer.start();
            playState = PLAY_STATE;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void pause() {
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
            playState = PAUSE_STATE;
        }
    }

    public void resume() {
        if (playState == PAUSE_STATE) {
            mediaPlayer.start();
            playState = PLAY_STATE;
        }
    }

    public void stop() {
        if (mediaPlayer != null) {
            try {
                mediaPlayer.stop();
                mediaPlayer.reset();
                playState = STOP_STATE;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    public boolean isPlaying() {
        if (mediaPlayer == null || instanceMedia == null) {
            return false;
        }
        return mediaPlayer.isPlaying();
    }


    private float playerVolume = 50.0f;

    //获取音量
    public float getPlayerVolume() {
        return playerVolume;
    }

    //设置音量
    public void setPlayerVolume(float playerVolume) {
        mediaPlayer.setVolume(playerVolume, playerVolume);  //左声道 右声道
        this.playerVolume = playerVolume;
    }

    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {
        stop();
        playState = STOP_STATE;
    }
}
